import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
// 引入路由
import router from './routers/index'
// 引入UI组件
import Vant from 'vant';
//引入组件样式
import 'vant/lib/index.css';
import VueCookies from 'vue3-cookies';
import Api from './api/index.js';
import $ from 'jquery';
window.$ = window.jQuery = $;

const app = createApp(App)
    .use(router)  // 路由挂载
    .use(Vant)
    .use(VueCookies)
    .use(Api)
    .mount('#app')

//为什么一个app能用，一个是proxy
router.beforeEach (async (to, from, next) => {
    //获取meta信息,判断如果为true就是需要登录，false不需要登录
    var IsLogin = to.meta.IsLogin ? to.meta.IsLogin : false

    if(IsLogin)
    {
        let LoginUser = app.$cookies.get("LoginUser") ? app.$cookies.get("LoginUser") : {}
        if (LoginUser){
            let busid = LoginUser.id ?? 0;
            let mobile = LoginUser.mobile ?? '';


            let result = await app.$api.check({
                busid: busid,
                mobile: mobile
            })
            if (result.code == 0){
                error("未登录请先去登录");
                app.$cookies.remove('LoginUser');
                next("/business/login");
                return false;
            }
            else
            {
                //这是有用的，为了刷新cookies，存入新的已经改过来的的用户信息
                app.$cookies.set('LoginUser',result.data)
                next()
            }
        }
        else
        {
            error("未登录请先去登录");
            next("/business/login");
            return false;
        }
    }
    else
    {
        next()
    }
})